package com.freeman.cement.controller;

import com.alibaba.fastjson.JSONObject;
import com.freeman.cement.annotation.NeedRole;
import com.freeman.cement.entity.ResponseResult;
import com.freeman.cement.entity.Sms;
import com.freeman.cement.exception.AppException;
import com.freeman.cement.exception.AppExceptionCodeMsg;
import com.freeman.cement.service.SmsService;
import com.freeman.cement.util.ConfigUtil;
import com.freeman.cement.util.HttpClientUtil;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.io.IOException;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

/**
 * 短信控制器
 * 用于发送短信
 * 用于查找短信发送记录
 */
@RestController
@RequestMapping("/sms")
@CrossOrigin    //允许跨域访问
public class SmsController {
    @Resource
    SmsService smsService;

    /**
     * 获取短信模块信号状态
     */
    @NeedRole("管理员||值班员")
    @RequestMapping(value = "/getModemStatus", method = RequestMethod.POST)
    public ResponseResult getModemStatus() {
        String GetModemStatusUrl = null;
        try {
            GetModemStatusUrl = ConfigUtil.readKey("GetModemStatusUrl");
        } catch (IOException e) {
            e.printStackTrace();
        }
        if(StringUtils.isNotBlank(GetModemStatusUrl)) {
            String rslt = HttpClientUtil.doGet(GetModemStatusUrl);
            if (StringUtils.isNotBlank(rslt)) {
                try {
                    Object jo = JSONObject.parseObject(rslt);
                    return ResponseResult.success(jo);
                } catch (Exception e) {
                    return ResponseResult.error(500011, rslt);
                }
            } else {
                throw new AppException(AppExceptionCodeMsg.CALL_SMS_SERVICE_ERROR.getCode(), AppExceptionCodeMsg.CALL_SMS_SERVICE_ERROR.getMsg() + "，服务地址：" + GetModemStatusUrl);
            }
        } else {
            throw new AppException(50061, "未配置短信模块状态查询地址");
        }
    }

    /**
     * 群发短信
     */
    @NeedRole("管理员||值班员")
    @RequestMapping(value = "/sendSmsMassWithCheck", method = RequestMethod.POST)
    public ResponseResult sendSmsMassWithCheck(
            @RequestParam String eventId,
            @RequestParam int senderId,
            @RequestParam String senderName,
            @RequestParam String recvList,
            @RequestParam String recvPhoneList,
            @RequestParam String content) {
        Map<String, String> map = new HashMap<>();
        map.put("phoneNumList", recvPhoneList);
        map.put("content", content);
        map.put("senderId", Integer.toString(senderId));
        map.put("senderName", senderName);

        String SendSmsMassWithCheckUrl;
        try {
            SendSmsMassWithCheckUrl = ConfigUtil.readKey("SendSmsMassWithCheckUrl");
        } catch (IOException e) {
            e.printStackTrace();
            SendSmsMassWithCheckUrl = null;
        }
        if (StringUtils.isNotBlank(SendSmsMassWithCheckUrl)) {
            String rslt = HttpClientUtil.doGet(SendSmsMassWithCheckUrl, map, 5000, 5000);
            if(StringUtils.isBlank(rslt)){
                rslt = "failed";
            }
            //rslt = com.cement.util.HttpUtils.get(SendSmsMassWithCheckUrl, map);
            //操作结果存入数据库
            Sms sms = new Sms(eventId, senderId, senderName, recvList, recvPhoneList, content, rslt, new Date());
            smsService.addSms(sms);
            if (StringUtils.isNotBlank(rslt) && !rslt.equals("failed")) {
                return ResponseResult.successMsg(rslt);
            } else {
                throw new AppException(AppExceptionCodeMsg.CALL_SMS_SERVICE_ERROR);
            }
        }
        else
        {
            return ResponseResult.error(500012, "调用失败，未配置短信服务地址");
        }
    }

    /**
     *  根据事件id分页查找短信记录
     */
    @NeedRole("管理员||值班员")
    @RequestMapping(value = "/selectSms", method = RequestMethod.POST)
    public ResponseResult selectSms(@RequestParam String eventId, @RequestParam int pageNo, @RequestParam int pageSize){
        return smsService.selectSmsByEventId(eventId, pageNo, pageSize);
    }

}
